package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.SetMeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * sky-take-out
 * com.sky.service.impl <BR>
 * C端购物车服务实现类
 * @author Ablecisi
 * @version 1.0
 * 2025/4/10
 * 15:20
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    private final ShoppingCartMapper shoppingCartMapper;
    private final DishMapper dishMapper;
    private final SetMealMapper setMealMapper;

    @Autowired
    public ShoppingCartServiceImpl(ShoppingCartMapper shoppingCartMapper, DishMapper dishMapper, SetMealMapper setMealMapper) {
        this.shoppingCartMapper = shoppingCartMapper;
        this.dishMapper = dishMapper;
        this.setMealMapper = setMealMapper;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车数据传输对象
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()) {
            // 如果购物车中已经存在该商品，则数量加1
            ShoppingCart existingCart = list.get(0);
            existingCart.setNumber(existingCart.getNumber() + 1);
            shoppingCartMapper.updateCart(existingCart);
        } else {
            // 如果购物车中不存在该商品，则添加新商品
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else if (setmealId != null) {
                SetMeal setMeal = setMealMapper.querySetMealById(setmealId);
                shoppingCart.setName(setMeal.getName());
                shoppingCart.setImage(setMeal.getImage());
                shoppingCart.setAmount(setMeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     * @return 购物车列表
     */
    @Override
    public List<ShoppingCart> list() {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 设置用户id
        shoppingCart.setUserId(userId);
        // 查询购物车列表
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 设置用户id
        shoppingCart.setUserId(userId);
        // 清空购物车
        shoppingCartMapper.clean(shoppingCart);
    }

    /**
     * 减少购物车商品数量
     * @param shoppingCartDTO 购物车数据传输对象
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置用户id
        shoppingCart.setUserId(userId);
        // 查询购物车列表
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart existingCart = list.get(0);
            if (existingCart.getNumber() > 1) {
                existingCart.setNumber(existingCart.getNumber() - 1);
                shoppingCartMapper.updateCart(existingCart);
            } else {
                // 如果数量为1，则删除该条购物车记录
                shoppingCartMapper.deleteById(existingCart.getId());
            }
        }
    }
}
